package com.supermarket.controller;


import com.supermarket.annotation.OperationLogAnnotation;
import com.supermarket.dto.LoginDto;
import com.supermarket.dto.UserPageQueryDto;
import com.supermarket.entity.User;
import com.supermarket.service.UserService;
import com.supermarket.service.ValidateCodeService;
import com.supermarket.utils.PageResult;
import com.supermarket.utils.Result;
import com.supermarket.vo.LoginVo;
import com.supermarket.vo.ValidateCodeVo;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.List;

@Slf4j
@Api(tags = "用户端")
@RequestMapping("/user")
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    private final ValidateCodeService validateCodeService;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    @ApiResponse(responseCode = "200", description = "登录成功")
    @OperationLogAnnotation(operation = "用户登录", operationType = "登录")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        log.info("用户登录{}", loginDto);
        return Result.success(userService.login(loginDto));
    }

    @GetMapping("/generateValidateCode")
    @Operation(summary = "获取图片验证码")
    @ApiResponse(responseCode = "200", description = "验证码生成成功")
    public Result<ValidateCodeVo> generateValidateCode() {
        return Result.success(validateCodeService.generateValidateCode());
    }

    @Operation(summary = "获取用户列表", description = "根据查询条件获取用户列表")
    @GetMapping("/list")
    public Result<PageResult> userList(@Parameter(description = "分页查询参数") UserPageQueryDto userPageQueryDto) {
        return Result.success(userService.list(userPageQueryDto));
    }

    @Operation(summary = "更新用户信息", description = "根据用户数据更新用户信息")
    @PutMapping
    @OperationLogAnnotation(operation = "更新用户信息", operationType = "更新")
    public Result update(@Parameter(description = "要更新的用户信息") @RequestBody User user) {
        log.info("修改用户{}", user);
        userService.updateUser(user);
        return Result.success();
    }

    @Operation(summary = "新增用户", description = "新增一个用户")
    @PostMapping("/add")
    @OperationLogAnnotation(operation = "新增用户", operationType = "新增")
    public Result add(@Parameter(description = "用户信息") @RequestBody User user) {
        userService.addUser(user);
        return Result.success();
    }

    @Operation(summary = "删除用户", description = "根据用户ID删除用户")
    @DeleteMapping("/{id}")
    @OperationLogAnnotation(operation = "删除用户", operationType = "删除")
    public Result delete(@Parameter(description = "用户ID") @PathVariable Long id) {
        log.info("删除的用户id{}", id);
        userService.deleteUserById(id);
        return Result.success();
    }

    @Operation(summary = "启用/禁用用户", description = "根据状态启用或禁用用户")
    @PutMapping("/status/{status}/{id}")
    @OperationLogAnnotation(operation = "修改用户状态", operationType = "更新")
    public Result enableOrDisable(
            @Parameter(description = "用户状态（1：启用，0：禁用）") @PathVariable Integer status,
            @Parameter(description = "用户ID") @PathVariable Long id) {
        log.info("启用/禁用员工 , {}, {}", status, id);
        userService.enableOrDisable(status, id);
        return Result.success();
    }

    @Operation(summary = "用户注销", description = "根据传入的 token 执行用户注销操作")
    @DeleteMapping("/logout")
    @OperationLogAnnotation(operation = "用户注销", operationType = "注销")
    public Result logout(
            @Parameter(description = "用户的认证 token") @RequestHeader(name = "token") String token) {
        userService.logout(token);
        return Result.success();
    }

    @GetMapping("/{id}")
    @OperationLogAnnotation(operation = "根据id查询用户", operationType = "根据id查询用户")
    public Result<User> getUserById(@PathVariable Long id) {
        log.info("根据id查询{}", id);
        User user = userService.getByUserId(id);
        return Result.success(user);
    }
}
